package com.example.sunsg.okhttptest.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.breadtrip.R;
import com.example.sunsg.okhttptest.Manager.FrescoManager;
import com.example.sunsg.okhttptest.bean.Image;
import com.example.sunsg.okhttptest.bean.LocalItem;
import com.example.sunsg.okhttptest.util.Util;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sunsg on 16/2/25.
 */
public class LifeStoryImagesAdapter extends BaseAdapter{
    private List<LocalItem> mItems;
    private Context mContext;
    private LayoutInflater mInflate;

    private int mImageWidth;

    public LifeStoryImagesAdapter(Context context){
        mItems = new ArrayList<>();
        mContext = context;
        mInflate = LayoutInflater.from(context);
        mImageWidth = Util.dp2px(context,66);
    }

    public void setmItems(List<LocalItem> imtes){
        mItems = imtes;
        notifyDataSetChanged();
    }

    public void addItem(LocalItem item){
        mItems.add(item);
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return mItems == null ? 0 : mItems.size();
    }

    @Override
    public LocalItem getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if(convertView == null){
            convertView = ((View) mInflate.inflate(R.layout.item_activity_life_story, parent, false));
            holder = new ViewHolder();
            holder.image = (SimpleDraweeView) convertView.findViewById(R.id.iv_image);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
            holder.tvCount = (TextView) convertView.findViewById(R.id.tv_count);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        LocalItem item = getItem(position);
        String title = item.mName;
        holder.tvTitle.setText(title);
        holder.tvCount.setText(mContext.getString(R.string.tv_photo_count, item.mCount));
        Image image = item.mImage;
        if (image != null && !TextUtils.isEmpty(image.getDataPath())) {
//            mImageFetcher.loadImage(image.mId, holder.mImageView);
            FrescoManager.loadFilePath(image.getDataPath()).resize(mImageWidth,mImageWidth).builder().into(holder.image);
        }

        return convertView;
    }

    class ViewHolder {
        public SimpleDraweeView image;
        public TextView tvTitle;
        public TextView tvCount;
    }

    public interface AdapterCallBack{
        void setSelect(int count);
    }
}
